<template>
  <div id="searchQuestion">
    <h2>题目浏览</h2>
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="title" label="名称" style="min-width: 240px">
        <a-input v-model="searchParams.title" placeholder="请输入题目名称" />
      </a-form-item>
      <a-form-item field="tags" label="标签" style="min-width: 240px">
        <a-input-tag v-model="searchParams.tags" placeholder="请输入题目标签" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" shape="round" @click="doSubmit">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table :columns="columns"
             :data="dataList"
             :pagination="{
               showTotal:true,
               pageSize:searchParams.pageSize,
               current:searchParams.current,
               total
             }"
             @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag,index) of record.tags" :key="index" color="green">
            {{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #acceptedRate="{ record }">
        {{
          `${record.submitNum ?
            record.acceptedNum / record.submitNum
            : "0"
          }%
           (${record.acceptedNum}/${record.submitNum})`
        }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doTopic(record)">做题</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import message from "@arco-design/web-vue/es/message";
import { Question, QuestionControllerService, QuestionQueryRequest } from "../../../generated";
import { useRouter } from "vue-router";
import moment from "moment";

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionQueryRequest>({
  title: "",
  tags: [],
  pageSize: 2,
  current: 1
});
//表格列名与数据字段Name映射定义
const columns = [
  {
    title: "题目编号",
    dataIndex: "id"

  }, {
    title: "题目标题",
    dataIndex: "title"
  }, {
    title: "标签",
    slotName: "tags"
  }, {
    title: "通过率",
    slotName: "acceptedRate"
  }, {
    title: "创建时间",
    slotName: "createTime"
  }, {
    title: "操作",
    slotName: "optional"
  }];
const router = useRouter();

/**
 * 加载题目信息
 */
const loadQuestionData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(searchParams.value);
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载题目信息失败," + res.message);
  }
};

/**
 * 分页页码改变事件
 * @param pageNo
 */
const onPageChange = (pageNo: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: pageNo
  };
};

/**
 * 监听searchParams值的变化,重新加载分页数据
 */
watchEffect(() => {
  loadQuestionData();
});

/**
 * 跳转做题路由
 * @param questionInfo 题目信息
 */
const doTopic = (questionInfo: Question) => {
  //路由跳转
  router.push({
    path: `/update/question/${questionInfo.id}`
  });
};

/**
 * 搜索按钮点击事件
 */
const doSubmit = () => {
  searchParams.value = {
    ...searchParams.value,
    //条件搜索之前重置页码为1,以免数据不足出现空页显示
    current: 1
  };
};

/**
 * 组件挂载函数,加载题目信息数据
 */
onMounted(() => {
  loadQuestionData();
});
</script>

<style scoped>
#searchQuestion {
  max-width: 80%;
  margin: 0 auto;
}
</style>
